function [status_quo ] = func_allocations_monthbymonth( IJipref,IJipref_choice,IJoutput, IJclassSize,IJVAm1, IJVAm2,IJfracDisadv, IJitidx,IJjtidx,IJschoolidx, IJappyear,titleI, vacancy_posting_month,month_first_app,yearselect,makebalanced,firstbest)

% This takes in information about teacher and school preferences.
% This is more flexible than other functions in terms of which side of the
% market is short or long
% makebalanced=1: market is balanced
% makeshort=1: makes schools short (100 schools vs.  178 teachers)
% makebalanced=0 & makeshort=0: teachers are short (296 positions vs. 178
% teachers)

% Output is: (1) output for disadvantaged; (2) frac_disadv among those
% matched; (3) frac disadv among unmatched (both weighted by number of
% students)
% status_quo=[va_disadv va_adv ];


% User supplies the year of the data to use
% User specifies whether make the schools and teachers balanced
%makebalanced=1;
%yearselect=2015;
%errors_on=1;
    %% go down to IJappyear==yearselect
    IJipref=IJipref(IJappyear==yearselect);
    IJipref_choice=IJipref_choice(IJappyear==yearselect);
    IJitidx2015=IJitidx(IJappyear==yearselect);
    IJjtidx2015=IJjtidx(IJappyear==yearselect);
    IJclassSize=IJclassSize(IJappyear==yearselect);
    IJoutput=IJoutput(IJappyear==yearselect);
    IJschoolidx=IJschoolidx(IJappyear==yearselect); 
    
    IJVAm1=IJVAm1(IJappyear==yearselect);
    IJVAm2=IJVAm2(IJappyear==yearselect);
    IJfracDisadv=IJfracDisadv(IJappyear==yearselect);
    vacancy_posting_month=vacancy_posting_month(IJappyear==yearselect);
    month_first_app=month_first_app(IJappyear==yearselect);
    titleI=titleI(IJappyear==yearselect);
    
    % redo indexes so that they start at 1 
    
    IJitidx=IJitidx2015-min(IJitidx2015)+1;
    IJjtidx=IJjtidx2015-min(IJjtidx2015)+1;
    
    if makebalanced==1
        imax=max(IJitidx);       
        jlist=unique(IJjtidx);
        jkeep=randperm(max(jlist),imax)';  % select a number of classrooms that equals the number of teachers
        jselect=ismember(IJjtidx,jkeep);  % generate a logical vector telling us which to keep
        
        % now apply the selector
        
        IJipref=IJipref( jselect);
        IJipref_choice=IJipref_choice( jselect);       
        IJclassSize=IJclassSize( jselect);
        IJoutput=IJoutput( jselect);
        IJVAm1=IJVAm1( jselect);
        IJVAm2=IJVAm2( jselect);
        IJfracDisadv= IJfracDisadv( jselect);
        IJitidx=IJitidx( jselect);
        IJjtidx= IJjtidx( jselect);
        IJschoolidx=IJschoolidx(jselect); 
        month_first_app=month_first_app(jselect);
        vacancy_posting_month=vacancy_posting_month(jselect);
        titleI=titleI(jselect);

        % sort j in sequential order (to renumber), then reorder everything
        % else
        [b1,Isorter]=sort(IJjtidx); % sort by j
        inbetween=repmat((1:imax),imax,1);
        IJjtidx=inbetween(:);
        
        IJipref=IJipref(Isorter);
        IJipref_choice=IJipref_choice(Isorter);        
        IJclassSize=IJclassSize( Isorter);
        IJoutput=IJoutput( Isorter);
        IJVAm1=IJVAm1( Isorter);
        IJVAm2=IJVAm2(Isorter);
        IJfracDisadv= IJfracDisadv(Isorter);
        IJitidx=IJitidx(Isorter);   
        IJschoolidx=IJschoolidx(Isorter); 
        month_first_app=month_first_app(Isorter);
        vacancy_posting_month=vacancy_posting_month(Isorter);
        titleI=titleI(Isorter);
     

    end
    
      
    
    
%% make the teacher preferences rectangular, and then rank

   % error_draws=evrnd(0,1,length(IJitidx),1);
    
    %if errors_on==1
            for i=1:length(IJitidx)
                 IJipref_sq(IJitidx(i,1),IJjtidx(i,1))=IJipref(i,1); %+error_draws(i,1); % add the type I errors
                 IJipref_choice_sq(IJitidx(i,1),IJjtidx(i,1))=IJipref_choice(i,1); % +error_draws(i,1); % add the type I errors
            end
    %end
    
    %if errors_on==0
     %    for i=1:length(IJitidx)
      %      IJipref_sq(IJitidx(i,1),IJjtidx(i,1))=IJipref(i,1)+error_draws(i,1); % even if errors aren't on, need to make the experienced utility have the errors
       %     IJipref_choice_sq(IJitidx(i,1),IJjtidx(i,1))=IJipref_choice(i,1);   % even if errors aren't on, need to make this square          
       %  end
    %end


   
   
   for i=1:length(IJitidx)
         IJjpref_sq(IJjtidx(i,1),IJitidx(i,1))=IJoutput(i,1);    % these are J X I (useful for DA)
         IJfracDisadv_ij(IJjtidx(i,1),IJitidx(i,1))=IJfracDisadv(i,1);  % these are J x 1 (useful for first best)   
         IJclassSize_ij(IJjtidx(i,1),IJitidx(i,1))=IJclassSize(i,1);  % these are I x J (useful for first best)
         IJijpref_sq(IJitidx(i,1),IJjtidx(i,1))=IJoutput(i,1);    % these are I X J (useful for first best)
         IJclassSize_sq(IJitidx(i,1),IJjtidx(i,1))=IJclassSize(i,1);  % these are I x J (useful for first best)
         IJVAm1_sq(IJitidx(i,1),IJjtidx(i,1))=IJVAm1(i,1);  % these are I X J (useful for first best)
         IJVAm2_sq(IJitidx(i,1),IJjtidx(i,1))=IJVAm2(i,1);  % these are I X J (useful for first best)
         IJfracDisadv_sq(IJitidx(i,1),IJjtidx(i,1))=IJfracDisadv(i,1);  % these are I X J (useful for first best)    
         vacancy_posting_month_sq(IJitidx(i,1),IJjtidx(i,1))=vacancy_posting_month(i,1);  % these are I X J (useful for first best)    
         month_first_app_sq(IJitidx(i,1),IJjtidx(i,1))=month_first_app(i,1);  % these are I X J (useful for first best)       
         titleI_sq(IJitidx(i,1),IJjtidx(i,1))=titleI(i,1);
         IJschoolidx_sq(IJitidx(i,1),IJjtidx(i,1))=IJschoolidx(i,1);

   end
   
   IJtype1num=(1-IJfracDisadv_sq).*IJclassSize_sq;
   IJtype2num=IJfracDisadv_sq.*IJclassSize_sq;
   
  %% complete index of positions and teachers
  
  teacherlist=(1:max(IJitidx))';
  poslist=(1:max(IJjtidx))';
   
   if firstbest==0
       
   %% clear running it all at once     

   % rank, and run DA
    [B1, IJirank]=sort(IJipref_choice_sq,2,'descend');  % this is sorted in descending order; so the first column in I is the most preferred of the teacher in the relevant row 
    IJirank_all=IJirank;  % want to be able to use the universal ranking 

    % make the school output vector rectangular, and then rank
    % note: this breaks ties in an arbitrary way 
       
   [B1, IJjrank]=sort(IJjpref_sq,2,'descend');  % this is sorted in descending order; so the first column in I is the most preferred of the school in the relevant row 
    
   % run DA
          
   [jass_iprop]= galeshapley_gender_unbalanced(IJirank,IJjrank);  % teach propose   
    % use the position list with the original index
   jass_iprop_wide_all=[jass_iprop, (1:length(jass_iprop))']; % teacher index, then school index (i,j), but the school index goes back to the original  
   
   %% clear for april
   
   
   % pick out things in april
   post_april=(vacancy_posting_month_sq(1,:)<=4);
   app_april=(month_first_app_sq(:,1)<=4);
   
   teacherlist_apr=teacherlist(app_april);
   poslist_apr=poslist(post_april);
   
   IJjpref_sq_apr=IJjpref_sq(post_april,app_april);
   IJipref_choice_sq_apr=IJipref_choice_sq(app_april, post_april);
   
   % rank, and run DA
    [B1, IJirank]=sort(IJipref_choice_sq_apr,2,'descend');  % this is sorted in descending order; so the first column in I is the most preferred of the teacher in the relevant row 

    % make the school output vector rectangular, and then rank
    % note: this breaks ties in an arbitrary way 
       
   [B1, IJjrank]=sort(IJjpref_sq_apr,2,'descend');  % this is sorted in descending order; so the first column in I is the most preferred of the school in the relevant row 
    
   % run DA, then convert back to original indices
          
   [jass_iprop_apr]= galeshapley_gender_unbalanced(IJirank,IJjrank);  % teach propose   
    % use the position list with the original index
   jass_iprop_apr_wide=[jass_iprop_apr, poslist_apr]; % teacher index, then school index (i,j), but the school index goes back to the original  
    % only keep rows where the school is matched (the teacher index is
    % non-zero)
   match=(jass_iprop_apr_wide(:,1)~=0);   
   jass_iprop_apr_wide=jass_iprop_apr_wide(match,:);
   % go back to original teach list
   int_apr=teacherlist_apr(jass_iprop_apr_wide(:,1));
   jass_iprop_apr_final=[int_apr, jass_iprop_apr_wide(:,2)];  % teacher, school
   
   
   %% now may/june
   % candidates are people who entered through june (including april)
   % but did not match in april
   
   teach_match_april=jass_iprop_apr_final(:,1);
   pos_match_april=jass_iprop_apr_final(:,2);
   
   post_june=(vacancy_posting_month_sq(1,:)<=6)';
   app_june=(month_first_app_sq(:,1)<=6);
   
   % if the position was filled/teacher hired in april, then not available
   for i=1:length(teach_match_april)
       app_june(teach_match_april(i,1))=0;
       post_june(pos_match_april(i,1))=0;
   end
   
   teacherlist_june=teacherlist(logical(app_june));
   poslist_june=poslist(logical(post_june));
   
   IJjpref_sq_june=IJjpref_sq(logical(post_june),logical(app_june));
   IJipref_choice_sq_june=IJipref_choice_sq(logical(app_june), logical(post_june));
   
   % rank, and run DA
    [B1, IJirank]=sort(IJipref_choice_sq_june,2,'descend');  % this is sorted in descending order; so the first column in I is the most preferred of the teacher in the relevant row 

    % make the school output vector rectangular, and then rank
    % note: this breaks ties in an arbitrary way 
       
   [B1, IJjrank]=sort(IJjpref_sq_june,2,'descend');  % this is sorted in descending order; so the first column in I is the most preferred of the school in the relevant row 
    
   % run DA, then convert back to original indices
          
   [jass_iprop_june]= galeshapley_gender_unbalanced(IJirank,IJjrank);  % teach propose   
    % use the position list with the original index
   jass_iprop_june_wide=[jass_iprop_june, poslist_june]; % teacher index, then school index (i,j), but the school index goes back to the original  
    % only keep rows where the school is matched (the teacher index is
    % non-zero)
   match=(jass_iprop_june_wide(:,1)~=0);   
   jass_iprop_june_wide=jass_iprop_june_wide(match,:);
   % go back to original teach list
   int_june=teacherlist_june(jass_iprop_june_wide(:,1));
   jass_iprop_june_final=[int_june, jass_iprop_june_wide(:,2)];  % teacher, school
      
  %% july/august 
  
     % candidates are people who entered through june (including april)
   % but did not match in april
   
   teach_match_june=[jass_iprop_apr_final(:,1); jass_iprop_june_final(:,1)] ;
   pos_match_june=[jass_iprop_apr_final(:,2);jass_iprop_june_final(:,2)] ;
   
   post_aug=ones(length(vacancy_posting_month_sq(1,:)),1);
   app_aug=ones(length(month_first_app_sq(:,1)),1);
   
   % if the position was filled/teacher hired in april, then not available
   for i=1:length(teach_match_june)
       app_aug(teach_match_june(i,1))=0;
       post_aug(pos_match_june(i,1))=0;
   end
   
   teacherlist_aug=teacherlist(logical(app_aug));
   poslist_aug=poslist(logical(post_aug));
   
   IJjpref_sq_aug=IJjpref_sq(logical(post_aug),logical(app_aug));
   IJipref_choice_sq_aug=IJipref_choice_sq(logical(app_aug), logical(post_aug));
   
   % rank, and run DA
    [B1, IJirank]=sort(IJipref_choice_sq_aug,2,'descend');  % this is sorted in descending order; so the first column in I is the most preferred of the teacher in the relevant row 

    % make the school output vector rectangular, and then rank
    % note: this breaks ties in an arbitrary way 
       
   [B1, IJjrank]=sort(IJjpref_sq_aug,2,'descend');  % this is sorted in descending order; so the first column in I is the most preferred of the school in the relevant row 
    
   % run DA, then convert back to original indices
          
   [jass_iprop_aug]= galeshapley_gender_unbalanced(IJirank,IJjrank);  % teach propose   
    % use the position list with the original index
   jass_iprop_aug_wide=[jass_iprop_aug, poslist_aug]; % teacher index, then school index (i,j), but the school index goes back to the original  
    % only keep rows where the school is matched (the teacher index is
    % non-zero)
   match=(jass_iprop_aug_wide(:,1)~=0);   
   jass_iprop_aug_wide=jass_iprop_aug_wide(match,:);
   % go back to original teach list
   int_aug=teacherlist_aug(jass_iprop_aug_wide(:,1));
   jass_iprop_aug_final=[int_aug, jass_iprop_aug_wide(:,2)];  % teacher, school
   
   
   %% then stack the final jass_iprop, and use this to compute output
   
   jass_iprop_wide=[jass_iprop_apr_final; jass_iprop_june_final; jass_iprop_aug_final];


   
   % for all     
  [va_disadv, va_adv] = func_output(IJVAm2_sq,IJtype2num,IJVAm1_sq,IJtype1num,jass_iprop_wide); 
   
   % for april
  [va_disadv_apr, va_adv_apr] = func_output(IJVAm2_sq,IJtype2num,IJVAm1_sq,IJtype1num,jass_iprop_apr_final);
     
    % for may/june
   [va_disadv_june, va_adv_june] = func_output(IJVAm2_sq,IJtype2num,IJVAm1_sq,IJtype1num,jass_iprop_june_final);
 
    % for july/august
   [va_disadv_aug, va_adv_aug] = func_output(IJVAm2_sq,IJtype2num,IJVAm1_sq,IJtype1num,jass_iprop_aug_final);
    
   %% now compute output twice. rather than splitting on when things clear
   % split on when vacancies are posted
   
   % create logical for posting in each of the periods
   
      post_april=(vacancy_posting_month_sq(1,:)<=4)';
      post_june=logical((vacancy_posting_month_sq(1,:)==5)'+(vacancy_posting_month_sq(1,:)==6)');
      post_aug=(vacancy_posting_month_sq(1,:)>6)';

      % now create indicators for whether this is at a title I school
      titleI=logical(titleI_sq(1,:))'; 
      nontitleI=logical(1-titleI);
      
      post_april_titleI=logical(post_april.*titleI);
      post_june_titleI=logical(post_june.*titleI);
      post_aug_titleI=logical(post_aug.*titleI);

      post_april_nontitleI=logical(post_april.*nontitleI);
      post_june_nontitleI=logical(post_june.*nontitleI);
      post_aug_nontitleI=logical(post_aug.*nontitleI);     
      
    % do this for "all"
    
       % for all     
  [va_disadv_all, va_adv_all] = func_output(IJVAm2_sq,IJtype2num,IJVAm1_sq,IJtype1num,jass_iprop_wide_all); 
   
   % for april
  [va_disadv_post_apr_all, va_adv_post_apr_all] = func_output(IJVAm2_sq,IJtype2num,IJVAm1_sq,IJtype1num,jass_iprop_wide_all(post_april,:));
     
    % for may/june
   [va_disadv_post_june_all, va_adv_post_june_all] = func_output(IJVAm2_sq,IJtype2num,IJVAm1_sq,IJtype1num,jass_iprop_wide_all(post_june,:));
 
    % for july/august
   [va_disadv_post_aug_all, va_adv_post_aug_all] = func_output(IJVAm2_sq,IJtype2num,IJVAm1_sq,IJtype1num,jass_iprop_wide_all(post_aug,:));
   
   % do this for "all": title I 
   
       % for all: title I      
  [va_disadv_all_titleI, va_adv_all_titleI] = func_output(IJVAm2_sq,IJtype2num,IJVAm1_sq,IJtype1num,jass_iprop_wide_all(titleI,:)); 
   
   % for april: title I
  [va_disadv_post_apr_all_titleI, va_adv_post_apr_all_titleI] = func_output(IJVAm2_sq,IJtype2num,IJVAm1_sq,IJtype1num,jass_iprop_wide_all(post_april_titleI,:));
     
    % for may/june: title I
   [va_disadv_post_june_all_titleI, va_adv_post_june_all_titleI] = func_output(IJVAm2_sq,IJtype2num,IJVAm1_sq,IJtype1num,jass_iprop_wide_all(post_june_titleI,:));
 
    % for july/august: title I
   [va_disadv_post_aug_all_titleI, va_adv_post_aug_all_titleI] = func_output(IJVAm2_sq,IJtype2num,IJVAm1_sq,IJtype1num,jass_iprop_wide_all(post_aug_titleI,:));
      
   
  % do this for "all": non-title I 
   
       % for all: non-title I      
  [va_disadv_all_nontitleI, va_adv_all_nontitleI] = func_output(IJVAm2_sq,IJtype2num,IJVAm1_sq,IJtype1num,jass_iprop_wide_all(nontitleI,:)); 
   
   % for april: title I
  [va_disadv_post_apr_all_nontitleI, va_adv_post_apr_all_nontitleI] = func_output(IJVAm2_sq,IJtype2num,IJVAm1_sq,IJtype1num,jass_iprop_wide_all(post_april_nontitleI,:));
     
    % for may/june: title I
   [va_disadv_post_june_all_nontitleI, va_adv_post_june_all_nontitleI] = func_output(IJVAm2_sq,IJtype2num,IJVAm1_sq,IJtype1num,jass_iprop_wide_all(post_june_nontitleI,:));
 
    % for july/august: title I
   [va_disadv_post_aug_all_nontitleI, va_adv_post_aug_all_nontitleI] = func_output(IJVAm2_sq,IJtype2num,IJVAm1_sq,IJtype1num,jass_iprop_wide_all(post_aug_nontitleI,:));
      
   
   
    % do this for month by month (mbm)
    
 
   % for april
  [va_disadv_post_apr_mbm, va_adv_post_apr_mbm] = func_output(IJVAm2_sq,IJtype2num,IJVAm1_sq,IJtype1num,jass_iprop_wide(post_april,:));
     
    % for may/june
   [va_disadv_post_june_mbm, va_adv_post_june_mbm] = func_output(IJVAm2_sq,IJtype2num,IJVAm1_sq,IJtype1num,jass_iprop_wide(post_june,:));
 
    % for july/august
   [va_disadv_post_aug_mbm, va_adv_post_aug_mbm] = func_output(IJVAm2_sq,IJtype2num,IJVAm1_sq,IJtype1num,jass_iprop_wide(post_aug,:));
   
   




   %% to teacher preferences
   
    
   app_april=(month_first_app_sq(:,1)<=4);
   app_june=(month_first_app_sq(:,1)<=6 & month_first_app_sq(:,1)>4 );
   app_aug=(month_first_app_sq(:,1)>6);
   
   % mbm
   jass_iprop_wide_teachsort=sortrows(jass_iprop_wide,1);  % sort this so that it is in order of the teacher index (the first column)
 
   [teach_rank_mbm_all]=func_rank(IJirank_all,jass_iprop_wide_teachsort);
   [teach_rank_mbm_apr]=func_rank(IJirank_all,jass_iprop_wide_teachsort(app_april,:));
   [teach_rank_mbm_june]=func_rank(IJirank_all,jass_iprop_wide_teachsort(app_june,:));
   [teach_rank_mbm_aug]=func_rank(IJirank_all,jass_iprop_wide_teachsort(app_aug,:));
   
   % all 

   jass_iprop_wide_teachsort=sortrows(jass_iprop_wide_all,1);  % sort this so that it is in order of the teacher index (the first column)

   [teach_rank_all_all]=func_rank(IJirank_all,jass_iprop_wide_teachsort);
   [teach_rank_all_apr]=func_rank(IJirank_all,jass_iprop_wide_teachsort(app_april,:));
   [teach_rank_all_june]=func_rank(IJirank_all,jass_iprop_wide_teachsort(app_june,:));
   [teach_rank_all_aug]=func_rank(IJirank_all,jass_iprop_wide_teachsort(app_aug,:)); 
   
   teach_rank=[ teach_rank_all_all teach_rank_all_apr teach_rank_all_june teach_rank_all_aug teach_rank_mbm_all teach_rank_mbm_apr teach_rank_mbm_june teach_rank_mbm_aug];
   end 
   
   
if firstbest==1
    
    %% clear for all  (this doesn't really make sense) 
     out_sq=-IJijpref_sq;
    [iind_best,jind_best] = linear_sum_assignment(out_sq); 
    % use position list in the original index
    jass_iprop_wide_all=[iind_best, jind_best]; % teacher, school
    
    
    
    %% clear for april
  
   % pick out things in april
   post_april=(vacancy_posting_month_sq(1,:)<=4);
   app_april=(month_first_app_sq(:,1)<=4);
   
   teacherlist_apr=teacherlist(app_april);
   poslist_apr=poslist(post_april);
   
    IJijpref_sq_apr=IJijpref_sq(app_april, post_april);
    
    out_sq=-IJijpref_sq_apr;
    [iind_best,jind_best] = linear_sum_assignment(out_sq); 
    % use position list in the original index
    jass_iprop_apr_final=[teacherlist_apr(iind_best), poslist_apr(jind_best)]; % teacher, school
    
  
   
   
   %% now may/june
   % candidates are people who entered through june (including april)
   % but did not match in april
   
   teach_match_april=jass_iprop_apr_final(:,1);
   pos_match_april=jass_iprop_apr_final(:,2);
   
   post_june=(vacancy_posting_month_sq(1,:)<=6)';
   app_june=(month_first_app_sq(:,1)<=6);
   
   % if the position was filled/teacher hired in april, then not available
   for i=1:length(teach_match_april)
       app_june(teach_match_april(i,1))=0;
       post_june(pos_match_april(i,1))=0;
   end
   
   teacherlist_june=teacherlist(logical(app_june));
   poslist_june=poslist(logical(post_june));
   
    IJijpref_sq_june=IJijpref_sq(logical(app_june), logical(post_june));
    
    out_sq=-IJijpref_sq_june;
    [iind_best,jind_best] = linear_sum_assignment(out_sq); 
    % use position list in the original index
    jass_iprop_june_final=[teacherlist_june(iind_best), poslist_june(jind_best)]; % teacher, school
    
   
  %% july/august 
  
     % candidates are people who entered through june (including april)
   % but did not match in april
   
   teach_match_june=[jass_iprop_apr_final(:,1); jass_iprop_june_final(:,1)] ;
   pos_match_june=[jass_iprop_apr_final(:,2);jass_iprop_june_final(:,2)] ;
   
   post_aug=ones(length(vacancy_posting_month_sq(1,:)),1);
   app_aug=ones(length(month_first_app_sq(:,1)),1);
   
   % if the position was filled/teacher hired in april, then not available
   for i=1:length(teach_match_june)
       app_aug(teach_match_june(i,1))=0;
       post_aug(pos_match_june(i,1))=0;
   end
   
   teacherlist_aug=teacherlist(logical(app_aug));
   poslist_aug=poslist(logical(post_aug));
   
    IJijpref_sq_aug=IJijpref_sq(logical(app_aug), logical(post_aug));
    
    out_sq=-IJijpref_sq_aug;
    [iind_best,jind_best] = linear_sum_assignment(out_sq); 
    % use position list in the original index
    jass_iprop_aug_final=[teacherlist_aug(iind_best), poslist_aug(jind_best)]; % teacher, school
    
   
 
   
   %% then stack the final jass_iprop, and use this to compute output
   
   jass_iprop_wide=[jass_iprop_apr_final; jass_iprop_june_final; jass_iprop_aug_final];
   
      % for all     
  [va_disadv, va_adv] = func_output(IJVAm2_sq,IJtype2num,IJVAm1_sq,IJtype1num,jass_iprop_wide); 
   
   % for april
  [va_disadv_apr, va_adv_apr] = func_output(IJVAm2_sq,IJtype2num,IJVAm1_sq,IJtype1num,jass_iprop_apr_final);
     
    % for may/june
   [va_disadv_june, va_adv_june] = func_output(IJVAm2_sq,IJtype2num,IJVAm1_sq,IJtype1num,jass_iprop_june_final);
 
    % for july/august
   [va_disadv_aug, va_adv_aug] = func_output(IJVAm2_sq,IJtype2num,IJVAm1_sq,IJtype1num,jass_iprop_aug_final);
    
   
   %% now compute output twice. rather than splitting on when things clear
   % split on when vacancies are posted
   
   % create logical for posting in each of the periods
   
      post_april=(vacancy_posting_month_sq(1,:)<=4)';
      post_june=logical((vacancy_posting_month_sq(1,:)==5)'+(vacancy_posting_month_sq(1,:)==6)');
      post_aug=(vacancy_posting_month_sq(1,:)>6)';

         % now create indicators for whether this is at a title I school
      titleI=logical(titleI_sq(1,:))'; 
      nontitleI=logical(1-titleI);
      
      post_april_titleI=logical(post_april.*titleI);
      post_june_titleI=logical(post_june.*titleI);
      post_aug_titleI=logical(post_aug.*titleI);

      post_april_nontitleI=logical(post_april.*nontitleI);
      post_june_nontitleI=logical(post_june.*nontitleI);
      post_aug_nontitleI=logical(post_aug.*nontitleI);     
      
    % do this for "all"
    
       % for all     
  [va_disadv_all, va_adv_all] = func_output(IJVAm2_sq,IJtype2num,IJVAm1_sq,IJtype1num,jass_iprop_wide_all); 
   
   % for april
  [va_disadv_post_apr_all, va_adv_post_apr_all] = func_output(IJVAm2_sq,IJtype2num,IJVAm1_sq,IJtype1num,jass_iprop_wide_all(post_april,:));
     
    % for may/june
   [va_disadv_post_june_all, va_adv_post_june_all] = func_output(IJVAm2_sq,IJtype2num,IJVAm1_sq,IJtype1num,jass_iprop_wide_all(post_june,:));
 
    % for july/august
   [va_disadv_post_aug_all, va_adv_post_aug_all] = func_output(IJVAm2_sq,IJtype2num,IJVAm1_sq,IJtype1num,jass_iprop_wide_all(post_aug,:));
   
   % do this for "all": title I 
   
       % for all: title I      
  [va_disadv_all_titleI, va_adv_all_titleI] = func_output(IJVAm2_sq,IJtype2num,IJVAm1_sq,IJtype1num,jass_iprop_wide_all(titleI,:)); 
   
   % for april: title I
  [va_disadv_post_apr_all_titleI, va_adv_post_apr_all_titleI] = func_output(IJVAm2_sq,IJtype2num,IJVAm1_sq,IJtype1num,jass_iprop_wide_all(post_april_titleI,:));
     
    % for may/june: title I
   [va_disadv_post_june_all_titleI, va_adv_post_june_all_titleI] = func_output(IJVAm2_sq,IJtype2num,IJVAm1_sq,IJtype1num,jass_iprop_wide_all(post_june_titleI,:));
 
    % for july/august: title I
   [va_disadv_post_aug_all_titleI, va_adv_post_aug_all_titleI] = func_output(IJVAm2_sq,IJtype2num,IJVAm1_sq,IJtype1num,jass_iprop_wide_all(post_aug_titleI,:));
      
   
  % do this for "all": non-title I 
   
       % for all: non-title I      
  [va_disadv_all_nontitleI, va_adv_all_nontitleI] = func_output(IJVAm2_sq,IJtype2num,IJVAm1_sq,IJtype1num,jass_iprop_wide_all(nontitleI,:)); 
   
   % for april: title I
  [va_disadv_post_apr_all_nontitleI, va_adv_post_apr_all_nontitleI] = func_output(IJVAm2_sq,IJtype2num,IJVAm1_sq,IJtype1num,jass_iprop_wide_all(post_april_nontitleI,:));
     
    % for may/june: title I
   [va_disadv_post_june_all_nontitleI, va_adv_post_june_all_nontitleI] = func_output(IJVAm2_sq,IJtype2num,IJVAm1_sq,IJtype1num,jass_iprop_wide_all(post_june_nontitleI,:));
 
    % for july/august: title I
   [va_disadv_post_aug_all_nontitleI, va_adv_post_aug_all_nontitleI] = func_output(IJVAm2_sq,IJtype2num,IJVAm1_sq,IJtype1num,jass_iprop_wide_all(post_aug_nontitleI,:));
      
   
    % do this for month by month (mbm)
    
 
   % for april
  [va_disadv_post_apr_mbm, va_adv_post_apr_mbm] = func_output(IJVAm2_sq,IJtype2num,IJVAm1_sq,IJtype1num,jass_iprop_wide(post_april,:));
     
    % for may/june
   [va_disadv_post_june_mbm, va_adv_post_june_mbm] = func_output(IJVAm2_sq,IJtype2num,IJVAm1_sq,IJtype1num,jass_iprop_wide(post_june,:));
 
    % for july/august
   [va_disadv_post_aug_mbm, va_adv_post_aug_mbm] = func_output(IJVAm2_sq,IJtype2num,IJVAm1_sq,IJtype1num,jass_iprop_wide(post_aug,:));
   
     teach_rank=zeros(1,8);
    diff_post=zeros(1,6); 
 

    
end

%% collect output

status_quo=[va_disadv va_adv va_disadv_apr  va_disadv_june va_disadv_aug va_adv_apr  va_adv_june va_adv_aug ]; 

status_quo_post_all=[va_disadv_all va_adv_all va_disadv_post_apr_all  va_disadv_post_june_all va_disadv_post_aug_all va_adv_post_apr_all  va_adv_post_june_all va_adv_post_aug_all ]; 

status_quo_post_all_titleI=[va_disadv_all_titleI va_adv_all_titleI va_disadv_post_apr_all_titleI  va_disadv_post_june_all_titleI va_disadv_post_aug_all_titleI va_adv_post_apr_all_titleI  va_adv_post_june_all_titleI va_adv_post_aug_all_titleI ]; 

status_quo_post_all_nontitleI=[va_disadv_all_nontitleI va_adv_all_nontitleI va_disadv_post_apr_all_nontitleI  va_disadv_post_june_all_nontitleI va_disadv_post_aug_all_nontitleI va_adv_post_apr_all_nontitleI  va_adv_post_june_all_nontitleI va_adv_post_aug_all_nontitleI ]; 

status_quo_post_mbm=[va_disadv va_adv va_disadv_post_apr_mbm  va_disadv_post_june_mbm va_disadv_post_aug_mbm va_adv_post_apr_mbm  va_adv_post_june_mbm va_adv_post_aug_mbm ]; 

status_quo=[status_quo status_quo_post_all status_quo_post_mbm status_quo_post_all_titleI status_quo_post_all_nontitleI teach_rank diff_post];
end

